In [1]:
import numpy as np
from scipy import misc
import scipy.io as scio
import matplotlib.pyplot as plt
import h5py
from sklearn.cluster import KMeans, MiniBatchKMeans, Birch, DBSCAN
In [3]:
with h5py.File('data.mat', 'r') as file:
    image = np.array(list(file['ImageRaw']))
# dict = scio.loadmat('data')
In [4]:
data.shape
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-4d6baf1a67bf> in <module>()
----> 1 data.shape

NameError: name 'data' is not defined
In [4]:
image.shape
data = np.reshape(image.T,(1000*1500,285))
In [5]:
data2 = np.reshape(image,(285,1000*1500)).T
In [83]:
image[:,:,1]
Out[83]:
array([[  82.,  107.,   22., ...,   74.,   63.,   81.],
       [  58.,   32.,   23., ...,  105.,   89.,  112.],
       [  73.,   39.,   23., ...,  124.,  104.,  130.],
       ..., 
       [   2.,    0.,    0., ...,   34.,    8.,    8.],
       [   0.,    0.,    0., ...,   22.,    0.,    0.],
       [   0.,    0.,    0., ...,   16.,    0.,    0.]])
In [6]:
k_means = KMeans(init='k-means++', n_clusters=5, n_init=1)
In [ ]:
k_means.fit(data)
In [7]:
k_means.fit(data2)
Out[7]:
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=5, n_init=1, n_jobs=1, precompute_distances='auto',
    random_state=None, tol=0.0001, verbose=0)
In [9]:
areaMap = np.array([[i,j] for i in range(1000) for j in range(1500)])
areaMap2 = np.array([[j,i] for i in range(1000) for j in range(1500)])
In [93]:
areaMap
Out[93]:
array([[   0,    0],
       [   1,    0],
       [   2,    0],
       ..., 
       [1497,  999],
       [1498,  999],
       [1499,  999]])
In [10]:
for i in range(5):
    plt.scatter(areaMap[k_means.labels_ == i,0], areaMap[k_means.labels_ == i,1], marker='.')
    plt.show()    
In [11]:
for i in range(5):
    plt.scatter(areaMap2[k_means.labels_ == i,0], areaMap2[k_means.labels_ == i,1], marker='.')
    plt.show()    
In [12]:
k_means10 = KMeans(init='k-means++', n_clusters=10, n_init=1)
In [13]:
k_means10.fit(data2)
Out[13]:
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=10, n_init=1, n_jobs=1, precompute_distances='auto',
    random_state=None, tol=0.0001, verbose=0)
In [99]:
np.unique(k_means10.labels_)
Out[99]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
In [16]:
for i in range(10):
    plt.plot(areaMap[k_means10.labels_ == i,0], areaMap[k_means10.labels_ == i,1], '.', markersize=1)
    plt.show()
In [17]:
for i in range(10):
    plt.plot(areaMap2[k_means10.labels_ == i,0], areaMap2[k_means10.labels_ == i,1], '.', markersize=1)
    plt.show()
In [18]:
k_means20 = KMeans(init='k-means++', n_clusters=20, n_init=1)
In [19]:
k_means20.fit(data2)
Out[19]:
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=20, n_init=1, n_jobs=1, precompute_distances='auto',
    random_state=None, tol=0.0001, verbose=0)
In [20]:
for i in range(20):
    plt.plot(areaMap[k_means20.labels_ == i,0], areaMap[k_means20.labels_ == i,1], '.',markersize=1)
    plt.show()
In [21]:
for i in range(20):
    plt.plot(areaMap2[k_means20.labels_ == i,0], areaMap2[k_means20.labels_ == i,1], '.',markersize=1)
    plt.show()
In [22]:
k_means30 = KMeans(init='k-means++', n_clusters=30, n_init=1)
In [23]:
k_means30.fit(data2)
Out[23]:
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=30, n_init=1, n_jobs=1, precompute_distances='auto',
    random_state=None, tol=0.0001, verbose=0)
In [24]:
for i in range(30):
    plt.plot(areaMap[k_means30.labels_ == i,0], areaMap[k_means30.labels_ == i,1], '.',markersize=1)
    plt.show()
In [41]:
plt.figure(figsize=(12,15))
plt.plot(areaMap[k_means30.labels_ == 7,0], areaMap[k_means30.labels_ == 7,1], 'g.',markersize=1)
plt.plot(areaMap[k_means30.labels_ == 24,0], areaMap[k_means30.labels_ == 24,1], 'g.',markersize=1)
plt.plot(areaMap[k_means30.labels_ == 10,0], areaMap[k_means30.labels_ == 10,1], 'b.',markersize=1)
plt.plot(areaMap[k_means30.labels_ == 5,0], areaMap[k_means30.labels_ == 5,1], 'y.',markersize=1)
plt.plot(areaMap[k_means30.labels_ == 14,0], areaMap[k_means30.labels_ == 14,1], 'k.',markersize=1)
plt.plot(areaMap[k_means30.labels_ == 26,0], areaMap[k_means30.labels_ == 26,1], 'k.',markersize=1)
plt.show()